/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StatisticsDialog.java
 *
 * Created on Apr 2, 2010, 9:56:44 PM
 */

package org.foment.gem.dialogs;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.print.PrinterException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ScrollPaneLayout;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.foment.joliage3.core.system.*;
import org.foment.joliage3.newt.KeyValue;
import org.foment.joliage3.sql.Database;
import org.foment.gem.beans.*;

/**
 *
 * @author Dusan
 */
public class StatisticsDialogFinal extends javax.swing.JFrame
{

    /** Creates new form StatisticsDialog */
    public StatisticsDialogFinal()
    {
        initComponents();
        setComponents();
        Font tableFont = new Font("Dialog", 0, 14);

        contentTable.setFont(tableFont);

        //columns.add("RowID");
        java.util.LinkedList<java.awt.Container> containerList = new java.util.LinkedList();
        containerList.add(tabbedPane);
        while (!containerList.isEmpty())
        {
            java.awt.Container current = containerList.getFirst();
            containerList.removeFirst();
            for (final Component component : current.getComponents())
            {
                // dodajemo sve atribute u tabelu, koju cemo prikazivati.
                if (JCheckBox.class.isInstance(component) && component.getName().startsWith("flg2"))
                {
                    // potom dodajemo slusac...
                    //System.out.println("Nadjeno: " + component.getName().substring(5));
                    // prvo dodajemo sve u vektor, inicijalno prikazujemo sve podatke.
                    columns.add(component.getName());
                    ((JCheckBox)component).addItemListener(new ItemListener() {
                        public void itemStateChanged(ItemEvent e) {
                        // ako je komponenta selektovana, dodajemo atribut
                        // ako je deselektovana, onda ga uklanjamo
                        if (((JCheckBox)component).isSelected() == true)
                        {
                            if (! columns.contains(component.getName()))
                            {
                                columns.add(component.getName());
                                System.out.println("Dodato " + columns.lastElement());
                            }
                        }
                        else
                        {
                            columns.remove(component.getName());
                            System.out.println("Uklonjeno " + component.getName());
                        }
                        
                    }
                    });
                }
                else if (JCheckBox.class.isInstance(component) && component.getName().startsWith("select"))
                {
                    System.out.println("Found " + component.getName());
                    ((JCheckBox)component).addItemListener(new ItemListener(){
                        public void itemStateChanged(ItemEvent e) {
                            
                        }
                    });
                }
                else if ((java.awt.Container.class.isInstance(component)))
                    containerList.addLast((java.awt.Container)component);
            }
        }
    }


    private void setComponents()
    {
        String items[] = {"Pozicije", "Predmeti"};
        choose = new JComboBox(items);
        pnlTitle.add(choose);
        
        pnlPositions = new PnlPositions();
        pnlCourses = new PnlCourses();
        
        pnlChoosePanel.add(pnlPositions, BorderLayout.NORTH);
        pnlChoosePanel.add(pnlCourses, BorderLayout.NORTH);

        choose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (((String)(choose.getSelectedItem())).equals("Pozicije"))
                {
                    pnlCourses.setVisible(false);
                    pnlPositions.setVisible(true);
                }
                else
                {
                    pnlPositions.setVisible(false);
                    pnlCourses.setVisible(true);
                }
            }
        });
    }
    private void setTableData(String query)
    {
        _model = new DefaultTableModel();
        // forsiramo da se prvo prikazu ime i prezime u tabeli. Estetika... :)
        if (columns.remove("flg2_Employee.EmployeeFirstName_Ime"))
            columns.add(0, "flg2_Employee.EmployeeFirstName_Ime");
        
        if (columns.remove("flg2_Employee.EmployeeSurname_Prezime") == true)
            columns.add(1, "flg2_Employee.EmployeeSurname_Prezime");
        
        for (String col : columns)
        {
            Vector<String> data = new Vector<String>();
            try
            {
                Connection conn = Database.getConnection();
                rs = conn.createStatement().executeQuery(query);
                while (rs.next())
                {
                    data.add(rs.getString(extractAttribute(col)));
                }
                rs.absolute(1);
                _model.addColumn(extractColumnTitle(col), data);
                rs.close();
                conn.close();
            }
            catch(SQLException e) {e.printStackTrace();}
        } 
        contentTable.setModel(_model);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        pnlContent = new javax.swing.JPanel();
        chboxName = new javax.swing.JCheckBox();
        chboxSurname = new javax.swing.JCheckBox();
        chboxJBMG = new javax.swing.JCheckBox();
        chboxLBO = new javax.swing.JCheckBox();
        chboxDistrict = new javax.swing.JCheckBox();
        chboxCourse = new javax.swing.JCheckBox();
        chboxJobType = new javax.swing.JCheckBox();
        pnlOptions = new javax.swing.JPanel();
        pnlEmployeeGender = new javax.swing.JPanel();
        chboxGenderFemale = new javax.swing.JCheckBox();
        chboxGenderMale = new javax.swing.JCheckBox();
        pnlEmployeeJobStatus = new javax.swing.JPanel();
        chboxZ = new javax.swing.JCheckBox();
        chboxP = new javax.swing.JCheckBox();
        chboxN = new javax.swing.JCheckBox();
        pnlAdvancedSearch = new javax.swing.JPanel();
        pnlTitle = new javax.swing.JPanel();
        pnlChoosePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        contentTable = new javax.swing.JTable();
        btnShow = new javax.swing.JButton();
        btnPrintStatistics = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Statistika");

        chboxName.setSelected(true);
        chboxName.setText("Ime");
        chboxName.setName("flg2_Employee.EmployeeFirstName_Ime"); // NOI18N

        chboxSurname.setSelected(true);
        chboxSurname.setText("Prezime");
        chboxSurname.setName("flg2_Employee.EmployeeSurname_Prezime"); // NOI18N

        chboxJBMG.setSelected(true);
        chboxJBMG.setText("JBMG");
        chboxJBMG.setName("flg2_Employee.EmployeeJMBG_JMBG"); // NOI18N

        chboxLBO.setSelected(true);
        chboxLBO.setText("LBO");
        chboxLBO.setName("flg2_Employee.EmployeeLBO_LBO"); // NOI18N

        chboxDistrict.setSelected(true);
        chboxDistrict.setText("Opština");
        chboxDistrict.setName("flg2_Employee.EmployeeDistrict_Opština"); // NOI18N

        chboxCourse.setText("Predmet");
        chboxCourse.setName("EmployeeCourse"); // NOI18N

        chboxJobType.setText("Pozicija");
        chboxJobType.setName("EmployeeJobType"); // NOI18N

        javax.swing.GroupLayout pnlContentLayout = new javax.swing.GroupLayout(pnlContent);
        pnlContent.setLayout(pnlContentLayout);
        pnlContentLayout.setHorizontalGroup(
            pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlContentLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chboxDistrict)
                    .addComponent(chboxLBO)
                    .addComponent(chboxJBMG)
                    .addGroup(pnlContentLayout.createSequentialGroup()
                        .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chboxName)
                            .addComponent(chboxSurname))
                        .addGap(22, 22, 22)
                        .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chboxJobType)
                            .addComponent(chboxCourse))))
                .addContainerGap(543, Short.MAX_VALUE))
        );
        pnlContentLayout.setVerticalGroup(
            pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlContentLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chboxName)
                    .addComponent(chboxCourse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chboxSurname)
                    .addComponent(chboxJobType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chboxJBMG)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chboxLBO)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chboxDistrict)
                .addContainerGap(140, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Sadržaj pretrage", pnlContent);

        pnlEmployeeGender.setName("Employee.EmployeeGender"); // NOI18N

        chboxGenderFemale.setSelected(true);
        chboxGenderFemale.setText("Žensko");
        chboxGenderFemale.setName("f"); // NOI18N

        chboxGenderMale.setSelected(true);
        chboxGenderMale.setText("Muško");
        chboxGenderMale.setName("m"); // NOI18N

        javax.swing.GroupLayout pnlEmployeeGenderLayout = new javax.swing.GroupLayout(pnlEmployeeGender);
        pnlEmployeeGender.setLayout(pnlEmployeeGenderLayout);
        pnlEmployeeGenderLayout.setHorizontalGroup(
            pnlEmployeeGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeGenderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlEmployeeGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chboxGenderMale, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chboxGenderFemale))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlEmployeeGenderLayout.setVerticalGroup(
            pnlEmployeeGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeGenderLayout.createSequentialGroup()
                .addComponent(chboxGenderMale, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chboxGenderFemale))
        );

        pnlEmployeeJobStatus.setName("Employee.EmployeeJobStatus"); // NOI18N

        chboxZ.setSelected(true);
        chboxZ.setText("Zaposlen");
        chboxZ.setName("z"); // NOI18N

        chboxP.setSelected(true);
        chboxP.setText("Penzionisan");
        chboxP.setName("p"); // NOI18N

        chboxN.setSelected(true);
        chboxN.setText("Na odredjeno");
        chboxN.setName("n"); // NOI18N

        javax.swing.GroupLayout pnlEmployeeJobStatusLayout = new javax.swing.GroupLayout(pnlEmployeeJobStatus);
        pnlEmployeeJobStatus.setLayout(pnlEmployeeJobStatusLayout);
        pnlEmployeeJobStatusLayout.setHorizontalGroup(
            pnlEmployeeJobStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeJobStatusLayout.createSequentialGroup()
                .addGroup(pnlEmployeeJobStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chboxZ)
                    .addComponent(chboxP)
                    .addComponent(chboxN))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        pnlEmployeeJobStatusLayout.setVerticalGroup(
            pnlEmployeeJobStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeJobStatusLayout.createSequentialGroup()
                .addComponent(chboxZ)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chboxP)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chboxN))
        );

        pnlAdvancedSearch.setBorder(javax.swing.BorderFactory.createTitledBorder("Napredna pretraga"));

        pnlChoosePanel.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout pnlAdvancedSearchLayout = new javax.swing.GroupLayout(pnlAdvancedSearch);
        pnlAdvancedSearch.setLayout(pnlAdvancedSearchLayout);
        pnlAdvancedSearchLayout.setHorizontalGroup(
            pnlAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAdvancedSearchLayout.createSequentialGroup()
                .addGroup(pnlAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlChoosePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                    .addComponent(pnlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlAdvancedSearchLayout.setVerticalGroup(
            pnlAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAdvancedSearchLayout.createSequentialGroup()
                .addComponent(pnlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlChoosePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnlOptionsLayout = new javax.swing.GroupLayout(pnlOptions);
        pnlOptions.setLayout(pnlOptionsLayout);
        pnlOptionsLayout.setHorizontalGroup(
            pnlOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlOptionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlEmployeeGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlEmployeeJobStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addComponent(pnlAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        pnlOptionsLayout.setVerticalGroup(
            pnlOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlOptionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlOptionsLayout.createSequentialGroup()
                        .addComponent(pnlAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnlOptionsLayout.createSequentialGroup()
                        .addComponent(pnlEmployeeGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pnlEmployeeJobStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(134, 134, 134))))
        );

        tabbedPane.addTab("Kriterijumi pretrage", pnlOptions);

        contentTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(contentTable);

        btnShow.setText("Prikaži");
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowActionPerformed(evt);
            }
        });

        btnPrintStatistics.setText("Štampaj");
        btnPrintStatistics.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPrintStatisticsMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 721, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnShow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnPrintStatistics, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(btnShow)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrintStatistics))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowActionPerformed
        // TODO add your handling code here:
        String query = generateQuery();
        //System.out.println(query);
        setTableData(query);
    }//GEN-LAST:event_btnShowActionPerformed

    private void btnPrintStatisticsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPrintStatisticsMouseClicked
        try {
            // pozovi stampanje
            MessageFormat header = new MessageFormat("Statistika zaposlenih");
            MessageFormat footer = new MessageFormat("Strana {0}");

            // first we add empty row in the table, followed by row which contains
            // total rows count information

            int rowCount    = contentTable.getRowCount();

            DefaultTableModel mod = (DefaultTableModel) contentTable.getModel();
            mod.insertRow(rowCount, new Object[]{""});
            mod.insertRow(rowCount+1, new Object[]{"Ukupno: " + rowCount});

            boolean result = contentTable.print(JTable.PrintMode.FIT_WIDTH,
                      header,
                      footer,
                      true,
                      null,
                      true,
                      null);

            if (result) {
                System.out.println("Printing success!");
            } else {
                System.out.println("Printing failed!");
            }

        } catch (PrinterException ex) {
            Logger.getLogger(StatisticsDialogFinal.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnPrintStatisticsMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new StatisticsDialogFinal().setVisible(true);
      }
    });
  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPrintStatistics;
    private javax.swing.JButton btnShow;
    private javax.swing.JCheckBox chboxCourse;
    private javax.swing.JCheckBox chboxDistrict;
    private javax.swing.JCheckBox chboxGenderFemale;
    private javax.swing.JCheckBox chboxGenderMale;
    private javax.swing.JCheckBox chboxJBMG;
    private javax.swing.JCheckBox chboxJobType;
    private javax.swing.JCheckBox chboxLBO;
    private javax.swing.JCheckBox chboxN;
    private javax.swing.JCheckBox chboxName;
    private javax.swing.JCheckBox chboxP;
    private javax.swing.JCheckBox chboxSurname;
    private javax.swing.JCheckBox chboxZ;
    private javax.swing.JTable contentTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlAdvancedSearch;
    private javax.swing.JPanel pnlChoosePanel;
    private javax.swing.JPanel pnlContent;
    private javax.swing.JPanel pnlEmployeeGender;
    private javax.swing.JPanel pnlEmployeeJobStatus;
    private javax.swing.JPanel pnlOptions;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables

    // vektor zaposlenih koje cemo u tabeli da prikazujemo.
    public static java.util.Vector<String> columns = new Vector<String>(); // vektor kolona
    DefaultTableModel _model = new DefaultTableModel();
    private ResultSet rs;
    private PnlPositions pnlPositions;
    private PnlCourses pnlCourses;
    JComboBox choose;
    


    // NOVI DEO....
    private String extractAttribute(String name)
    {
        // ime vezano za checkbox je oblika
        // flg_AttrName_ColumnName, pa treba izolovati Attrname
        int first = name.indexOf("_");
        int last = name.lastIndexOf("_");
        if (first >= 0 && last != first)
            return name.substring(first+1, last);
        return name;
    }
    
    private String extractColumnTitle(String name)
    {
         // ovde treba izolovati vrednost, (value) atributa
        int last = name.lastIndexOf("_");
        if (last >= 0)
            return name.substring(last+1);
        return name;
    }
    private String generateQuery()
    {
        // prvo biramo atribute u SELECT-u
        //String select = "";
        String select = "";
        String cond = "";
        String where = " WHERE ";
        String from = "";
        for (Component c : pnlContent.getComponents())
        {
            if (JCheckBox.class.isInstance(c) && c.getName().startsWith("flg2"))
                if (((JCheckBox)c).isSelected())
                {
                    select += extractAttribute(c.getName());
                    select += ", ";
                }
        }

        int last = select.lastIndexOf(",");
        // odsecamo zadnji ","
        if (last > 0)
            select = select.substring(0, last);

        if (select.equals(""))
            return null; // nije nista selektovano!!
        
        // sad vrsimo spajanje tabela po uslovima koje smo izabrali.
        /*
        select += " FROM Employee, EmployeeJobType, EmployeeCourse, JobType WHERE " +
                " Employee.EmployeeID = EmployeeJobType.EmployeeID AND " +
                " EmployeeJobType.EmployeeID = EmployeeCourse.EmployeeID AND" +
                " EmployeeJobType.JobTypeID = JobType.JobTypeID AND ";

         */
        
         from += " Employee ";
        
        for (Component c : pnlOptions.getComponents())
        {
            
            if (!JPanel.class.isInstance(c) || c.getName() == null)
                continue;
            // Ime panela je ime atributa. u zavistnosti od selektovatnih
            // checkbox-ova, dodajemo uslove spajanja
            cond += c.getName();
            cond += " IN ( ";

            for (Component cc : ((JPanel)c).getComponents())
            {
                if (JCheckBox.class.isInstance(cc))
                {
                    cond += " '";
                    if (((JCheckBox)cc).isSelected())
                    {
                        cond += cc.getName();
                    }
                    cond += "' ,";
                }
                
            }

            // odsecamo poslednji zarez
            last = cond.lastIndexOf(",");
            // ali tako da bude POSLE poslednjeg AND!!
            if (last > 0 && last >= cond.lastIndexOf("AND"))
                cond = cond.substring(0, last);
            cond += " ) AND ";
        }
        cond = cond.substring(0, cond.lastIndexOf("AND"));
        
        String queryPos = pnlPositions.getQuery();
        String queryCourse = pnlCourses.getQuery();

        if (!queryPos.equals(""))
        {
            select += " ,JobType.JobTypeTitle ";
            from += ", EmployeeJobType, JobType ";
            cond += (" AND " + queryPos);
            where += " Employee.EmployeeID = EmployeeJobType.EmployeeID AND JobType.JobTypeID = EmployeeJobType.JobTypeID AND " +
                    "EmployeeJobType.EmployeeJobTypeActive = '1' AND ";
            // generisali smo upit. Kolonu dodajemo u tabelu ako vec ne postoji i AKO je chbox cekiran
            if (!columns.contains("flg2_JobType.JobTypeTitle_Pozicija") )
            {
                if (chboxJobType.isSelected())
                    columns.add("flg2_JobType.JobTypeTitle_Pozicija");
            }
            // ako nije cekiran, bez obzira na upit, uklanjamo kolonu, jer necemo da prikazujemo
            else if (!chboxJobType.isSelected())
                columns.remove("flg2_JobType.JobTypeTitle_Pozicija");
        }

        else
            if (columns.contains("flg2_JobType.JobTypeTitle_Pozicija"))
                columns.remove("flg2_JobType.JobTypeTitle_Pozicija");

        if (!queryCourse.equals(""))
        {
            select += " , Course.courseTitle ";
            from += ", EmployeeCourse, course ";
            where += " EmployeeCourse.EmployeeID = Employee.EmployeeID AND Course.CourseID = EmployeeCourse.courseID AND ";
            cond += (" AND " + queryCourse);
            if (!columns.contains("flg2_course.courseTitle_Predmet"))
            {
                if (chboxCourse.isSelected())
                    columns.add("flg2_course.courseTitle_Predmet");
            }
            else if (!chboxCourse.isSelected())
                columns.remove("flg2_course.courseTitle_Predmet");
        }
        else
            if (columns.contains("flg2_course.courseTitle_Predmet"))
                columns.remove("flg2_course.courseTitle_Predmet");
        
        return "SELECT " + select + " FROM " + from + where + cond + " ORDER BY Employee.EmployeeFirstName ; ";

    }

}